package com.mrduan.otgfilemanager.ui.video;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.bumptech.glide.Glide;
import com.github.mjdev.libaums.fs.UsbFile;
import com.mrduan.otgfilemanager.Config;
import com.mrduan.otgfilemanager.R;
import com.mrduan.otgfilemanager.ui.main.fragment.TwoScreenFragment;
import com.shike.baselibrary.activity.BaseActivity;

import java.io.File;
import java.io.IOException;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

public class VideoPlayActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());

        initBindWidget();

        initListener();

        try {
            initData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getContentView() {
        return R.layout.activity_video_play;
    }

    public void initBindWidget() {

    }

    public void initListener() {

    }

    public void initData() throws IOException {

        UsbFile file = null;
        UsbFile fileImg = null;

        UsbFile[] files = TwoScreenFragment.usbFile.listFiles();
        for (UsbFile f : files) {
            if (Config.isVideo(f.getName())) {
                file = f;
            } else {
                fileImg = f;
            }
        }

        if (file != null && fileImg != null) {
            JZVideoPlayerStandard jzVideoPlayerStandard = (JZVideoPlayerStandard) findViewById(R.id.videoplayer);
            jzVideoPlayerStandard.startFullscreen(this, JZVideoPlayerStandard.class, Config.getUrl(file), file.getName());
            Glide.with(this)
                    .load(Config.getUrl(fileImg))
                    .into(jzVideoPlayerStandard.thumbImageView);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
}
